/**
 * 首页模块
 * 默认模块名: main
 * @return {[object]}  [ 返回一个对象 ]
 */
let storage = bui.storage();

let org;
let uiList;
let height;
let height2;
let language
let translate

loader.define(function(require, exports, module, global) {
    var pageview = {};

    // 模块初始化定义
    pageview.init = function() {
        language = storage.get("language", 0)
        translate = global.translate
        if (!language) {
            language = 'chinese_simplified'
        }

        try {
            translate.selectLanguageTag.show = false;
            let curLanguage = translate.language.getCurrent()
            if (curLanguage !== language) {
                translate.changeLanguage(language)
            }
            translate.execute();
        }catch (e) {
            console.log("切换语言报错")
        }

        org = storage.get("org",0)
        height = $(window).height() -75;
        height2 = $(document).height() -75;
        router.$("#scrollList").hide()

        router.$("#searchFh").click(function () {
            let prevInfo = bui.history.getCurrentPrev()
            router.replace({url: prevInfo.url});
        })

        router.$('#search').bind('keypress',function(event){//监听sim卡回车事件
            if(event.keyCode == "13")
            {
                let str = $(this).val()
                if (str) {
                    router.$("#scrollList").show()
                    if (uiList) {
                        uiList.replace({
                            data:{city: org.id,
                                country: org.parent,
                                title: str
                            }
                        });
                    } else {
                        scList(str, global.api.getSearchArticleList)
                    }
                } else {
                    router.$("#scrollList").hide()
                    bui.alert("关键词不可为空！")
                }
            }
        });
        router.$('#sear').click(function(event){//监听sim卡回车事件
            let str = router.$("#search").val()
            if (str) {
                router.$("#scrollList").show()
                if (uiList) {
                    uiList.replace({
                        data:{city: org.id,
                            country: org.parent,
                            title: str
                        }
                    });
                } else {
                    scList(str, global.api.getSearchArticleList)
                }
            } else {
                router.$("#scrollList").hide()
                bui.alert("关键词不可为空！")
            }
        });
    }

    // 初始化
    pageview.init();

    // 输出模块
    return pageview;
})

function formatDate(time) {
    let date = new Date(time * 1000);

    let Y = date.getFullYear() + '-';
    let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
    let D = (date.getDate() < 10 ? '0'+(date.getDate()) : date.getDate()) + ' ';
    let h = (date.getHours() < 10 ? '0'+(date.getHours()) : date.getHours()) + ':';
    let m = (date.getMinutes() < 10 ? '0'+(date.getMinutes()) : date.getMinutes());
    let s = (date.getSeconds() < 10 ? '0'+(date.getSeconds()) : date.getSeconds());
    return M+D+h+m
}

function scList(search, url) {
    uiList = bui.list({
        id: "#scrollList",
        url: url,
        height: height,
        pageSize: 10, // 当pageSize 小于返回的数据大小的时候,则认为是最后一页,接口返回的数据最好能返回空数组,而不是null
        data: {
            city: org.id,
            country: org.parent,
            title: search
        },
        //如果分页的字段名不一样,通过field重新定义
        field: {
            page: "page",
            size: "limit",
            data: "data"
        },
        callback: function(e) {
            // e.target 为你当前点击的元素
            // e.currentTarget 为你当前点击的handle 整行
            let id = router.$(e.currentTarget).attr("id")
            let name = router.$(e.currentTarget).attr("name")
            router.load({ url: "pages/articleInfo/articleInfo.html", param: {id: id, glUserid: name, tzSign: '0'}});

        },
        template: function(data) {
            let fontSize = '0.3rem'
            let fontSize2 = '0.2rem'
            if (language !== "chinese_simplified") {
                fontSize = '0.22rem'
                fontSize2 = '0.12rem'
            }

            var html = "";
            data.forEach(function(el, index) {
                if (index == 0) {
                    router.$("#searchTitle").html("共找到 "+el.total+" 条信息")
                }
                search = router.$("#search").val()
                let titles = el.title.split(search)

                html += '<li class="bui-btn" id="'+el.id+'" name="'+el.glUserid+'">\n'
                html += '<p>'
                titles.forEach(function(el, index) {
                    html += '<span class="titleText" style="font-size: '+fontSize+'">'+el+'</span>'
                    if (index < titles.length - 1) {
                        html += '<span class="titleText" style="color: red; font-size: '+fontSize+'">'+search+'</span>'
                    }
                })
                html += '<span style="font-size: 0.2rem;margin-left: 10px">'+formatDate(el.ptime)+'</span>'

                html += '</p>'
                if (el.contentText) {
                    let xzLength = 100
                    let contentTexts = el.contentText.split(search)
                    if (el.contentText.length < xzLength) {
                        contentTexts.forEach(function(el, index) {
                            html += '<span class="titleText" style="color: rgb(120,120,120);font-size: '+fontSize2+'">'+el+'</span>'
                            if (index < contentTexts.length - 1) {
                                html += '<span class="titleText" style="color: red; font-size: '+fontSize2+'">'+search+'</span>'
                            }
                        })
                    } else {
                        let len = search.length
                        contentTexts.forEach(function(el, index) {
                            if (len <= xzLength) {
                                if (el.length > xzLength - len) {
                                    let str = el.substring(el.length - (xzLength - len), el.length)
                                    html += '<span class="titleText" style="color: rgb(120,120,120);font-size: '+fontSize2+'">...'+str+'</span>'
                                } else {
                                    html += '<span class="titleText" style="color: rgb(120,120,120);font-size: '+fontSize2+'">'+el+'</span>'
                                }
                                len += el.length
                                if (index < contentTexts.length - 1) {
                                    html += '<span class="titleText" style="color: red; font-size: '+fontSize2+'">'+search+'</span>'
                                }
                            }
                        })
                    }
                }
                html += '<p>'
                html += '                        </li>'
            });
            if (data.length == 0) {
                router.$("#searchTitle").html("共找到 0 条信息")
            }

            setTimeout(function () {
                try {
                    translate.execute();
                }catch (e) {
                    console.log("切换语言报错")
                }
            },1000)
            return html;
        }
    })
}
